package com.svmc.app.task.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.svmc.app.task.activity.R;
import com.svmc.app.task.model.RingtoneModel;

public class ListSelectRingAdapter extends BaseAdapter{
	Context mcontext;
	public static int mSelectedVariation;
	ArrayList<RingtoneModel> listRing = new ArrayList<RingtoneModel>();
	private LayoutInflater mInflater;
	
	 public ListSelectRingAdapter(Context context, ArrayList<RingtoneModel> ringtone,int selectedVariation) {
	    	mcontext = context;
	    	listRing = ringtone;
	    	mInflater = LayoutInflater.from(context);
	    	mSelectedVariation = selectedVariation;
	    	
	    }

	@Override
	public int getCount() {
	
		return listRing.size();
	}

	@Override
	public Object getItem(int position) {
		return listRing.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertview, ViewGroup parent) {
		ViewholderRing holder ;
		if(convertview == null){
			mInflater = (LayoutInflater) mcontext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			 convertview = mInflater.inflate(R.layout.item_selectring, null);
			 holder = new ViewholderRing();
			 holder.titleRing = (TextView)convertview.findViewById(R.id.itemselectring_txt_titlering);
			 holder.ischoise =(RadioButton)convertview.findViewById(R.id.itemselect_radiobutton_choise);
			 convertview.setTag(holder);
		}else {
			holder =(ViewholderRing)convertview.getTag();
		}
		holder.titleRing.setText(listRing.get(position).getTitle());

		if(position==mSelectedVariation){
			holder.ischoise.setChecked(true);
		}else {
			holder.ischoise.setChecked(false);
		}
		
		convertview.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
              mSelectedVariation = position;
              ListSelectRingAdapter.this.notifyDataSetChanged();
              Toast.makeText(mcontext, listRing.get(position).getPath(), 0).show();
            }
        });

		return convertview;
	}
	
	static class ViewholderRing {
		TextView titleRing;
		RadioButton ischoise;
		
		
	}

}
